<template>
  <view class="container">
    <!-- <input v-if="isUseScanGun" class="jk-footer" style="width: 1px;" :value="form.code" :focus="isFocus"
			maxlength="-1" @input="input" @focus="focus" @blur="blur" @confirm="confirm" /> -->
    <view class="form">
      <uni-forms ref="form" :modelValue="form" label-position="top">
        <uni-forms-item :label="`${$t('returnwarehouse.form.name')}`">
          <uni-easyinput v-model="form.name" disabled />
        </uni-forms-item>
        <uni-forms-item :label="`${$t('returnwarehouse.form.batchnumber')}`">
          <uni-easyinput v-model="form.batchnumber" disabled />
        </uni-forms-item>
        <uni-forms-item :label="`${$t('returnwarehouse.form.expiryDate')}`">
          <uni-easyinput v-model="form.expiryDate" disabled />
        </uni-forms-item>
        <uni-forms-item :label="`${$t('returnwarehouse.form.num')}`">
          <UniNumberBox
            :value="form.num"
            @change="numchange"
            :min="1"
            @foucs="formfocus"
            @blur="formblur"
            :focus="numFocus"
            :max="form.totalNum"
          >
          </UniNumberBox>
        </uni-forms-item>
        <uni-forms-item :label="`${$t('returnwarehouse.form.storeNum')}`">
          <view>
            <uni-easyinput v-model="form.storeNum" disabled="" />
          </view>
        </uni-forms-item>
        <uni-forms-item :label="`${$t('returnwarehouse.form.totalNum')}`">
          <view>
            <uni-easyinput v-model="form.totalNum" disabled="" />
          </view>
        </uni-forms-item>
        <uni-forms-item :label="`${$t('returnwarehouse.form.warehouse')}`">
          <view>
            <uni-easyinput v-model="form.warehouse" disabled="" />
          </view>
        </uni-forms-item>
        <uni-forms-item :label="`${$t('returnwarehouse.form.shelfName')}`">
          <view>
            <uni-easyinput v-model="form.shelfName" disabled="" />
          </view>
        </uni-forms-item>
        <!-- <uni-forms-item :label="`${$t('returnwarehouse.form.code')}`" name="code">
					<view class="code-item">
						<uni-easyinput v-model="form.code" @focus="formfocus" @blur="formblur" />
						<button type="primary" @click="sendCode">{{ $t('returnwarehouse.form.codebtn') }}</button>
					</view>

				</uni-forms-item> -->
      </uni-forms>
    </view>
    <view class="bottom-btn">
      <button class="button" type="primary" @click="store">
        {{ $t('returnwarehouse.form.btn') }}
      </button>
    </view>
    <view>
      <keyboard-listener @keydown="scancodelisten"></keyboard-listener>
    </view>
  </view>
</template>

<script>
import api from '../../common/api.js';
import common from '../../common/common.js';
import UniNumberBox from '@/components/uni-number-box/uni-number-box.vue';
import KeyboardListener from 'uniapp-keyboard-listener';
import { reactive, ref, nextTick } from 'vue';
import {
  onLoad,
  onShow,
  onUnload,
  onHide,
  onBackPress
} from '@dcloudio/uni-app';
export default {
  components: {
    UniNumberBox,
    KeyboardListener
  },
  data() {
    return {
      isUseScanGun: false,
      timer: null,
      index: 0,
      rules: {},
      id: '',
      batchnumber: '', //批次号
      table: [],
      list: [
        { value: 0, text: '篮球' },
        { value: 1, text: '足球' },
        { value: 2, text: '游泳' }
      ],
      isFocus: false,
      numFocus: false,
      warehouseList: [],
      aaaa: true,
      pdacode: '',
      form: {
        id: '',
        code: '',
        name: '',
        warehouse: '',
        batchnumber: '123',
        date: '2024-10-10',
        storeNum: 0,
        num: 0,
        totalNum: 20,
        inboundOrderId: '',
        expiryDate: ''
      },
      code: '',
      changeInput: false
    };
  },
  onNavigationBarButtonTap(e) {
    console.log(e, '----e---');
    this.code = '';
    let that = this;
    uni.scanCode({
      success: async function (res) {
        console.log('条码类型：' + res.scanType);
        console.log('条码内容：' + res.result);
        that.code = res.result;
        await that.sendCode();
      }
    });
  },
  async onLoad(option) {
    this.form.id = option.id;
    console.log('上一个页面传递过来的参数', option);
    console.log('id', option.id);
    console.log('item', option.item);
    console.log(this.form);
    console.log(this.form.id !== undefined);

    if (this.form.id !== undefined) {
      await this.findbyId();
      this.isFocus = true;
      // this.timer = setInterval(() => { uni.hideKeyboard(); }, 50)
    } else {
      console.log('----------navigateBack-------------');
      uni.navigateBack();
    }
  },
  async onUnload() {
    clearInterval(this.timer);
    this.timer = null;
  },
  methods: {
    async scancodelisten(e) {
      let keyCode = e.keyCode;
      // console.log(e, '-----e---')
      if (
        (keyCode >= 65 && keyCode <= 90) ||
        (keyCode >= 97 && keyCode <= 122)
      ) {
        console.log('字母输入:', e.key);
        this.pdacode = this.pdacode + e.key;
      } else if (keyCode >= 48 && keyCode <= 57) {
        console.log('数字输入:', e.key);
        console.log(this.numFocus, 'numFocus');
        this.pdacode = this.pdacode + e.key;
      } else if (keyCode === 13) {
        this.code = this.pdacode;
        if (this.code !== '') {
          await this.sendCode();
        }
        this.pdacode = '';
      } else {
        console.log('其他输入:', e.key);
      }

      console.log(this.pdacode, '-----this.pdacode---');
    },
    numchange(e) {
      this.form.num = Number(e);
    },
    async findbyId() {
      console.log('-----------------------');
      uni.showLoading();
      let ret = await common.scanInboundCode(this.form.id);
      console.log(ret, '------ret');
      if (ret.code === 0 && ret.data) {
        this.form.id = ret.data.id;
        this.form.name = ret.data.drugName;
        this.form.batchnumber = ret.data.batchNumber;
        this.form.warehouse = ret.data.warehouseName;
        this.form.shelfName = ret.data.shelfName;
        this.form.storeNum = ret.data.confirmNum;
        this.form.totalNum = ret.data.num;
        this.form.inboundOrderId = ret.data.inboundOrderId;
        let dateFormat = common.formatDate(ret.data.expiryDate);
        this.form.expiryDate = dateFormat;
        uni.hideLoading();
      } else {
        uni.showToast({
          title: ret.message,
          icon: 'none'
        });
        setTimeout(() => {
          uni.navigateBack();
        }, 2000);
      }
    },
    formfocus(e) {
      this.pdacode = '';
      setTimeout(() => {
        this.isFocus = false;
        this.numFocus = true;
      }, 0);
    },
    formblur(e) {
      console.log(e.detail.value);
      this.pdacode = '';
      this.numFocus = false;
    },
    input(e) {
      console.log('键盘输入:' + e.detail.value);
      this.isFocus = false;
    },
    focus() {
      console.log('输入框聚焦');
    },
    blur() {
      console.log('输入框失去焦点');
      this.isFocus = true;
    },
    confirm(e) {
      console.log('点击完成按钮');
      var str = e.detail.value;
      this.form.code = str; //截取后13位
      uni.showToast({
        title: this.form.code
      });
    },
    async findWareHouse() {
      // let ret = await api.findWareHouse()
      this.warehouseList = [{ text: 'A2' }, { text: 'A1' }];
    },
    chooseWarehouse() {
      uni.hideKeyboard();
      console.log('chooseWarehouse');
      this.aaaa = true;
    },
    close() {
      console.log('close');
      // this.aaaa = false
    },
    async store() {
      uni.showLoading();
      let ret = await api.batchInbound({
        inboundOrderDetailId: this.form.id, //入库单明细id
        totalNum: this.form.num //入库数量
      });
      if (ret.code === 0 && ret.success) {
        uni.hideLoading();
        uni.navigateBack();
      } else {
        uni.hideLoading();
        this.$nextTick(() => {
          uni.showToast({
            title: ret.msg,
            icon: 'none'
          });
        });
      }
    },
    async sendCode() {
      uni.showLoading();
      let ret = await common.scanInboundCode(this.code);
      if (ret.code === 0 && ret.data) {
        let checkName = ret.data.drugName === this.form.name;
        let checkWarehouseName = ret.data.warehouseName === this.form.warehouse;
        let checkshelfName = ret.data.shelfName === this.form.shelfName;
        let checkinboundOrderId =
          ret.data.inboundOrderId === this.form.inboundOrderId;
        if (
          checkName &&
          checkWarehouseName &&
          checkshelfName &&
          checkinboundOrderId
        ) {
          this.form.num++;
          uni.hideLoading();
        } else {
          uni.hideLoading();
          this.$nextTick(() => {
            uni.showToast({
              title: this.$t('scan.dataerror'),
              icon: 'none'
            });
          });
        }
      } else {
        uni.hideLoading();
        this.$nextTick(() => {
          uni.showToast({
            title: ret.message,
            icon: 'none'
          });
        });
      }
    }
  }
};
</script>

<style>
.form {
  margin-bottom: 40rpx;
}

.bottom-btn {
  position: absolute;
  width: 100%;
  bottom: 0;
}

.code-item {
  display: flex;
  flex: 1;
}
</style>
